const api = require("../../http/api")
const AUTH = require('../../utils/auth')

// pages/cart/cart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart: []
    },
    del(e) {
        // console.log(e)
        const key = e.currentTarget.dataset.key
        this.delfn(key)
    },
    delfn(key) {

        const token = wx.getStorageSync('token')
        // const key=e.currentTarget.dataset.key
        wx.showModal({
            title: '提示',
            content: '确定要删除该商品吗？',
            success: async (res) => {
                if (res.confirm) {
                    const res = await api.getgwcdel({
                        token,
                        key
                    })
                    this.getgwc()
                } else {
                    return
                }
            }
        })

    },
    async jia(e) {
        const index = e.currentTarget.dataset.index;
        const item = this.data.cart[index]
        const number = item.number + 1
        const token = wx.getStorageSync('token')
        const res = await api.getgwcjia({
            token: token,
            key: item.key,
            number: number
        })
        // console.log(res)
        this.getgwc()
    },
    async jian(e) {
        const index = e.currentTarget.dataset.index;
        const item = this.data.cart[index]
        const number = item.number - 1
        if (item.number <= 0) {
            wx.showModal({
                title: '提示',
                content: '确定要删除该商品吗？',
                success: async (res) => {
                    if (res.confirm) {
                        const res = await api.getgwcjian({
                            token,
                            key
                        })
                        this.getgwc()
                    } else {
                        return
                    }
                }
            })

        }
        const token = wx.getStorageSync('token')
        const res = await api.getgwcjian({
            token: token,
            key: item.key,
            number: number
        })
        // console.log(res)
        this.getgwc()
    },
    async radioClick(e) {
        var index = e.currentTarget.dataset.index;
        var item = this.data.cart[index]
        console.log(item)
        const token = wx.getStorageSync('token')
        const res = await api.getgwczt({
            token: token,
            key: item.key,
            selected: !item.selected
        })
        console.log(res)
        this.getgwc()
    },
    async getgwc() {
        const token = wx.getStorageSync('token')
        const res = await api.getgwc(token)
        console.log(res)
        if (res.code == 0) {
            this.data.cart = res.data.items
            this.setData({
                cart: this.data.cart,
                price: res.data.price,
                shopList: res.data.shopList
            })
        } else {
            this.setData({
                cart: [],
                price: 0,
                shopList: []
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getgwc()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})